package com.zengdada.authority.utils.excel.export.style;

import com.zengdada.authority.utils.excel.export.style.exception.NotUseMethodException;
import org.apache.poi.ss.usermodel.*;


/**
 * @author 12614
 * @contact 1261438511@qq.com
 * @date 2020-06-07 10:36
 * @describe 帮助设置 表格样式
 */
public class MyCellUtil {

    public static final MyCellStyleSize EMPTY_CELL_STYLE_SIZE = new EmptyMyCellStyleSize();
    public static final MyCellStyle FULL_CENTER_CELL_STYLE = new FullCenterMyCellStyle();


    public static CellStyle setCellStyle(Cell cell, MyCellStyle cellStyle) {
        Workbook workbook = cell.getSheet().getWorkbook();

        CellStyle originalStyle = cell.getCellStyle();
        Font originalFont = workbook.getFontAt(originalStyle.getFontIndexAsInt());

        CellStyle newStyle = null;
//        Map<String, Object> values = getFormatProperties(originalStyle);
//        putAll(properties, values);
        MyCellStyle mNewStyle = new MyCellStyle(originalStyle, originalFont).merge(cellStyle);

        // iwrapTextndex seems like what index the cellstyle is in the list of styles for a workbook.
        // not good to compare on!
        int numberCellStyles = workbook.getNumCellStyles();
        MyCellStyle tmpStyle;
        CellStyle wbStyle;
        Font wbFont;
        for (int i = 0; i < numberCellStyles; i++) {
            wbStyle = workbook.getCellStyleAt(i);
            wbFont = workbook.getFontAt(wbStyle.getFontIndexAsInt());
//            Map<String, Object> wbStyleMap = getFormatProperties(wbStyle);
            tmpStyle = new MyCellStyle(wbStyle, wbFont);
            // the desired style already exists in the workbook. Use the existing style.
            if (tmpStyle.equals(mNewStyle)) {
                newStyle = wbStyle;
                break;
            }
        }

        // the desired style does not exist in the workbook. Create a new style with desired properties.
        if (newStyle == null) {
            newStyle = workbook.createCellStyle();
            //            setFormatProperties(newStyle, workbook, values);
            mNewStyle.initCellStyle(newStyle);
            if (mNewStyle.getFont() != null) {
                MyFontUtil.setCellStyleFont(workbook, newStyle, mNewStyle.getFont());
            }
        }

        cell.setCellStyle(newStyle);
        return newStyle;
    }

    static class EmptyMyCellStyleSize extends MyCellStyleSize {

        @Override
        public MyCellStyleSize setWidth(short width) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyleSize setHeight(short height) {
            throw new NotUseMethodException();
        }
    }

    static class FullCenterMyCellStyle extends MyCellStyle {

        private HorizontalAlignment alignment;
        private VerticalAlignment verticalAlignment;

        public FullCenterMyCellStyle() {
            this.alignment = HorizontalAlignment.CENTER_SELECTION;
            this.verticalAlignment = VerticalAlignment.CENTER;
        }

        @Override
        public HorizontalAlignment getAlignment() {
            return alignment;
        }

        @Override
        public VerticalAlignment getVerticalAlignment() {
            return verticalAlignment;
        }

        @Override
        public MyCellStyle merge(MyCellStyle cellStyle) {
            return new MyCellStyle().merge(this).merge(cellStyle);
        }

        @Override
        public MyCellStyle setFillPattern(FillPatternType fillPattern) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setFillBackgroundColor(Short fillBackgroundColor) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setFillForegroundColor(Short fillForegroundColor) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setAlignment(HorizontalAlignment alignment) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setVerticalAlignment(VerticalAlignment verticalAlignment) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setBorderLeft(BorderStyle borderLeft) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setBorderRight(BorderStyle borderRight) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setBorderTop(BorderStyle borderTop) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setBorderBottom(BorderStyle borderBottom) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setLeftBorderColor(Short leftBorderColor) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setRightBorderColor(Short rightBorderColor) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setTopBorderColor(Short topBorderColor) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setBottomBorderColor(Short bottomBorderColor) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setDataFormat(Short dataFormat) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setRotation(Short rotation) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setIndention(Short indention) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setHidden(Boolean hidden) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setLocked(Boolean locked) {
            throw new NotUseMethodException();
        }

        @Override
        public MyCellStyle setWrapText(Boolean wrapText) {
            throw new NotUseMethodException();
        }
    }
}
